/*
 * Copyright © 2009 Perseus Project - Tufts University <http://www.perseus.tufts.edu>
 *
 * This file is part of UtilPerseus.
 *
 * UtilPerseus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * UtilPerseus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with UtilPerseus.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.himeros.util;

import java.util.*;

/**
 * Comparator for Double objects.
 *
 * @author Federico Boschetti <federico.boschetti.73@gmail.com>
 */
public class DoubleComparator implements Comparator {
        private boolean reverse=false;

        /**
         * Determine ascending or descending order.
         */
        public void switchReverse(){
            reverse=reverse?false:true;
        }


        /**
         *
         * @param o1 first Double to compare.
         * @param o2 second Double to compare.
         * @return -1 if o1&lt;o2; 0 if o1==o2; 1 if o1&gt;o2
         */
        public int compare(Object o1,Object o2)
        {
                if( ((Double)((Map.Entry)o1).getValue()).doubleValue() > ((Double)((Map.Entry)o2).getValue()).doubleValue() ){
                        if(!reverse){
                            return(1);
                        }else{
                            return(-1);
                        }
                }else if( ((Double)((Map.Entry)o1).getValue()).doubleValue() < ((Double)((Map.Entry)o2).getValue()).doubleValue() ){
                        if(!reverse){
                            return(-1);
                        }else{
                            return(1);
                        }
                }else{
                        return(0);
                }
        }
}
